﻿#include "DemonWolf.h"
#include "../../MirEnvir/Envir.h"
#include "../../../Shared/Functions/Functions.h"
#include "../../../Shared/ServerPackets.h"
#include "../../../Shared/Data/Stat.h"
#include "../DelayedAction.h"
#include "../../../Shared/Enums.h"
#include "../MapObject.h"
#include "../../MirEnvir/Map.h"

using namespace Server::MirDatabase;
using namespace Server::MirEnvir;
namespace S = ServerPackets;

namespace Server::MirObjects::Monsters
{

    DemonWolf::DemonWolf(MonsterInfo *info) : MonsterObject(info)
    {
    }

    bool DemonWolf::InAttackRange()
    {
        if (getTarget()->getCurrentMap() != getCurrentMap())
        {
            return false;
        }
        if (getTarget()->getCurrentLocation() == getCurrentLocation())
        {
            return false;
        }

        int x = std::abs(getTarget()->getCurrentLocation()->X - getCurrentLocation()->X);
        int y = std::abs(getTarget()->getCurrentLocation()->Y - getCurrentLocation()->Y);

        if (x > 2 || y > 2)
        {
            return false;
        }

        return (x <= 1 && y <= 1) || (x == y || x % 2 == y % 2);
    }

    void DemonWolf::ProcessSearch()
    {
        if (getEnvir()->getTime() < SearchTime)
        {
            return;
        }

        MonsterObject::ProcessSearch();

        Pack = FindPackNearby(5);
    }

    void DemonWolf::Attack()
    {
        if (!getTarget()->IsAttackTarget(this))
        {
            setTarget(nullptr);
            return;
        }

        setDirection(Functions::DirectionFromPoint(getCurrentLocation(), getTarget()->getCurrentLocation()));
        bool ranged = getCurrentLocation() == getTarget()->getCurrentLocation() || !Functions::InRange(getCurrentLocation(), getTarget()->getCurrentLocation(), 1);

        ShockTime = 0;

        ActionTime = getEnvir()->getTime() + 300;
        AttackTime = getEnvir()->getTime() + AttackSpeed;

        int damageMultiplier = (Pack.size() > _MaxPackSize ? _MaxPackSize : Pack.size()) + 1;

        if (Info->Effect == 1)
        {
            if (!ranged && getEnvir()->Random->Next(4) > 0)
            {
                S::ObjectAttack *tempVar = new S::ObjectAttack();
                tempVar->ObjectID = ObjectID;
                tempVar->Direction = getDirection();
                tempVar->Location = getCurrentLocation();
                tempVar->Type = 1;
                Broadcast(tempVar);

                int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC] * damageMultiplier);
                if (damage == 0)
                {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
                    return;
                }

                DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 500, {getTarget(), damage, DefenceType::MACAgility});
                ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar' statement was not added since tempVar was passed to a method or constructor. Handle memory management manually.
            }
            else
            {
                S::ObjectAttack *tempVar2 = new S::ObjectAttack();
                tempVar2->ObjectID = ObjectID;
                tempVar2->Direction = getDirection();
                tempVar2->Location = getCurrentLocation();
                tempVar2->Type = 0;
                Broadcast(tempVar2);

                LineAttack(3);

                MoveTo(getTarget()->getCurrentLocation());

//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar2' statement was not added since tempVar2 was passed to a method or constructor. Handle memory management manually.
            }
        }
        else
        {
            S::ObjectAttack *tempVar3 = new S::ObjectAttack();
            tempVar3->ObjectID = ObjectID;
            tempVar3->Direction = getDirection();
            tempVar3->Location = getCurrentLocation();
            tempVar3->Type = 0;
            Broadcast(tempVar3);

            int damage = GetAttackPower(Stats[Stat::MinDC], Stats[Stat::MaxDC] * damageMultiplier);
            if (damage == 0)
            {
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar3' statement was not added since tempVar3 was passed to a method or constructor. Handle memory management manually.
                return;
            }

            DelayedAction *action = new DelayedAction(DelayedType::Damage, getEnvir()->getTime() + 300, {getTarget(), damage, DefenceType::MACAgility});
            ActionList.push_back(action);

//C# TO C++ CONVERTER TODO TASK: A 'delete action' statement was not added since action was passed to a method or constructor. Handle memory management manually.
//C# TO C++ CONVERTER TODO TASK: A 'delete tempVar3' statement was not added since tempVar3 was passed to a method or constructor. Handle memory management manually.
        }
    }

    void DemonWolf::CompleteAttack(std::vector<std::any> &data)
    {
        MapObject *target = std::any_cast<MapObject*>(data[0]);
        int damage = std::any_cast<int>(data[1]);
        DefenceType defence = std::any_cast<DefenceType>(data[2]);

        if (target == nullptr || !target->IsAttackTarget(this) || target->getCurrentMap() != getCurrentMap() || target->Node == nullptr)
        {
            return;
        }

        if (target->Attacked(this, damage, defence) <= 0)
        {
            return;
        }

        if (Info->Effect == 1)
        {
            PoisonTarget(target, 4, 5, PoisonType::Bleeding, 1000);
        }
    }

    std::vector<MonsterObject*> DemonWolf::FindPackNearby(int distance)
    {
        std::vector<MonsterObject*> pack;

        for (int d = 0; d <= distance; d++)
        {
            for (int y = getCurrentLocation()->Y - d; y <= getCurrentLocation()->Y + d; y++)
            {
                if (y < 0)
                {
                    continue;
                }
                if (y >= getCurrentMap()->Height)
                {
                    break;
                }

                for (int x = getCurrentLocation()->X - d; x <= getCurrentLocation()->X + d; x += std::abs(y - getCurrentLocation()->Y) == d ? 1 : d * 2)
                {
                    if (x < 0)
                    {
                        continue;
                    }
                    if (x >= getCurrentMap()->Width)
                    {
                        break;
                    }
                    if (!getCurrentMap()->ValidPoint(x, y))
                    {
                        continue;
                    }
                    Cell *cell = getCurrentMap()->GetCell(x, y);
                    if (cell->Objects.empty())
                    {
                        continue;
                    }

                    for (int i = 0; i < cell->Objects.size(); i++)
                    {
                        MapObject *ob = cell->Objects[i];
                        switch (ob->getRace())
                        {
                            case ObjectType::Monster:
                                if (ob == this || ob->Dead)
                                {
                                    continue;
                                }
                                if (ob->IsAttackTarget(this))
                                {
                                    continue;
                                }
                                if ((static_cast<MonsterObject*>(ob))->Info->AI != Info->AI)
                                {
                                    continue;
                                }
                                pack.push_back(static_cast<MonsterObject*>(ob));
                                continue;
                        }
                    }
                }
            }
        }

        return pack;
    }
}
